package com.lyx.crm.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyx.crm.base.BaseService;
import com.lyx.crm.dao.CusDevPlanMapper;
import com.lyx.crm.dao.SaleChanceMapper;
import com.lyx.crm.query.CusDevPlanQuery;
import com.lyx.crm.utils.AssertUtil;
import com.lyx.crm.vo.CusDevPlan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Package: com.lyx.crm.service
 * @ClassName: CusDevPlanService
 * @Author: lyx
 * @CreateTime: 2021/7/27 11:19
 * @Description:
 */
@Service
public class CusDevPlanService extends BaseService<CusDevPlan, Integer> {

    @Autowired
    private CusDevPlanMapper cusDevPlanMapper;
    @Autowired
    private SaleChanceMapper saleChanceMapper;

    /**
     * 多条件查询营销机会  （返回的数据格式必须满足LayUi中数据表格要求的格式）
     *
     * @param cusDevPlanQuery
     * @return
     */
    public Map<String, Object> querySaleChanceByParams(CusDevPlanQuery cusDevPlanQuery) {
        Map<String, Object> map = new HashMap<>();

        //开启分页
        PageHelper.startPage(cusDevPlanQuery.getPage(), cusDevPlanQuery.getLimit());
        //得到对应分页对象
        PageInfo<CusDevPlan> pageInfo = new PageInfo<>(cusDevPlanMapper.selectByParams(cusDevPlanQuery));

        //设置map对象
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());

        return map;
    }

    /**
     * 添加开发项
     *
     * @param cusDevPlan
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addCusDevPlan(CusDevPlan cusDevPlan) {
        /* 1. 参数校验  */
        checkCusDevPlanParams(cusDevPlan);

        /* 2. 设置参数的默认值 */
        // 是否有效    默认有效
        cusDevPlan.setIsValid(1);
        // 创建时间    系统当前时间
        cusDevPlan.setCreateDate(new Date());
        // 修改时间    系统当前时间
        cusDevPlan.setUpdateDate(new Date());

        /* 3. 执行添加操作，判断受影响的行数 */
        AssertUtil.isTrue(cusDevPlanMapper.insertSelective(cusDevPlan) != 1, "计划项数据添加失败！");
    }


    /**
     * 更新计划项
     *
     * @param cusDevPlan
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateCusDevPlan(CusDevPlan cusDevPlan) {
        /*1.校验参数*/
        AssertUtil.isTrue(null == cusDevPlan.getId() ||
                cusDevPlanMapper.selectByPrimaryKey(cusDevPlan.getId()) == null, "数据异常，请重试。");
        checkCusDevPlanParams(cusDevPlan);

        // 修改时间    系统当前时间
        cusDevPlan.setUpdateDate(new Date());

        /* 3. 执行添加操作，判断受影响的行数 */
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1, "计划项数据更新失败！");
    }


    private void checkCusDevPlanParams(CusDevPlan cusDevPlan) {
        //营销机会ID，非空，数据存在
        Integer sId = cusDevPlan.getSaleChanceId();
        AssertUtil.isTrue(null == sId || null == saleChanceMapper.selectByPrimaryKey(sId), "数据异常，请重试");
        //计划项内容  非空
        AssertUtil.isTrue(StrUtil.isBlank(cusDevPlan.getPlanItem()), "计划项内容不能为空");

        //计划时间 非空
        AssertUtil.isTrue(cusDevPlan.getPlanDate() == null, "计划时间不能为空！");

    }

    /**
     * 删除计划项
     *
     * @param id
     */
    public void deleteCusDevPlan(Integer id) {
        AssertUtil.isTrue(null == id, "待删除数据不存在");
        CusDevPlan cusDevPlan = cusDevPlanMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(ObjectUtil.isEmpty(cusDevPlan), "该计划项不存在！");

        //设置记录无效
        cusDevPlan.setIsValid(0);
        //设置修改时间
        cusDevPlan.setUpdateDate(new Date());
        //更新cusDevPlan
        AssertUtil.isTrue(cusDevPlanMapper.updateByPrimaryKeySelective(cusDevPlan) != 1, "计划项数据删除失败");

    }

}
